
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h" 

IWDT_HandleTypeDef IWDT_Handle;


/******************************************************************************
*@brief : APP_Test
*@param : none
*@return: none
******************************************************************************/
void APP_Test(void)
{
    uint32_t reload;
    uint32_t freq;
    uint32_t resetTime;
    RCC_OscInitTypeDef      RCC_OscInitStruct = {0};
    
    printfS("IWDT Function Test!\n");
    
	/* Enable: RC32K */
    RCC_OscInitStruct.OscType = RCC_OSCTYPE_RC32K;
    RCC_OscInitStruct.RC32K = ENABLE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        while (1);
    }  
    
	/* Enable: IWDT reset causes system reset */
	__HAL_RCC_IWDT_RESET_ENABLE();
    
    reload = 2000;
    
    __HAL_RCC_EXTI_CLK_ENABLE();
    
	/* Configure IWDT */
    IWDT_Handle.Instance = IWDT;
    IWDT_Handle.Init.Prescaler  = IWDT_CLOCK_PRESCALER_16;
    IWDT_Handle.Init.Reload     = reload;
    IWDT_Handle.Init.Window     = reload;
    IWDT_Handle.Init.Wakeup     = reload;
    
    HAL_IWDT_Init(&IWDT_Handle);

    freq = HAL_RCC_GetRC32KVal() / 16;
    resetTime = reload * 1000 / freq;
    
    HAL_IWDT_Refresh(&IWDT_Handle);
    
    printfS("Start to test IWDT, Reset mode: 1s period!\n"); 
    
    while(1)
    {
        /* Refresh the IWDT 50 ms before the reset time */ 
        HAL_Delay(resetTime - 50);

        printfS("feed IWDT time : %u\r\n", HAL_GetTick());

        HAL_IWDT_Refresh(&IWDT_Handle);
        
    };
}


